package com.hikeen.launcherx.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hikeen.launcherx.R;
import com.hikeen.launcherx.bean.AppInfos;
import com.hikeen.launcherx.utils.FileImageUtils;


import java.io.File;
import java.util.List;

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.VH> {

    private View mView;
    private List<AppInfos> mlist;
    private Context mContext;
    private VH mVH;
    private OnItemClickListener listener;
    private OnItemLongClickListener longClickListener;
    private AppInfos appInfo = null;


    private Bitmap bitmap;

    public TestAdapter(Context context, List<AppInfos> list) {
        this.mlist = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_apps, parent, false);

        mVH = new VH(mView);
        return mVH;
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        appInfo = mlist.get(position);


        //获取到的app的图标和名字进行设置，也可以通过判断包名来修改图片
        if (appInfo!=null){
            if (TextUtils.isEmpty(appInfo.getAppName())){
                holder.item_layout.setVisibility(View.INVISIBLE);
            }else {
                holder.item_layout.setVisibility(View.VISIBLE);
            }
            holder.iv_apptb.setBackground(appInfo.getAppIcon());
            holder.name.setText(appInfo.getAppName());


            String s = FileImageUtils.savefilePath(mContext, appInfo.getAppName());
            File filePic = new File(s);
            if (filePic!=null){
                Glide.with(mContext).load(filePic).into(holder.icon);
//                holder.icon.setBackground(s);
            }else {
                Glide.with(mContext).load(R.drawable.black_selector).into(holder.icon);
            }
//


        }


        final int itemposition = position;
        holder.item_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(itemposition,v);
                }
            }
        });

        holder.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(itemposition,v);
                }
            }
        });
//        holder.itemlayout.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                if (longClickListener != null) {
//                    longClickListener.onClick(itemposition);
//                }
//                return true;
//            }
//        });
    }

    @Override
    public int getItemCount() {

        return mlist.size()>0?mlist.size():0;
    }

    //点击接口
    public interface OnItemClickListener {
        void onClick(int position, View v);
    }

    //公共的点击方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    //长按的点击接口
    public interface OnItemLongClickListener {
        void onClick(int position);
    }

    //公共的长按的接口
    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public class VH extends RecyclerView.ViewHolder {
        public LinearLayout item_layout;
        public LinearLayout item_close;
        public TextView name;
        public ImageView icon;
        public ImageView iv_apptb;

        public VH(View itemView) {
            super(itemView);
            item_layout = (LinearLayout) itemView.findViewById(R.id.item_layout);
            icon = (ImageView) itemView.findViewById(R.id.item_app_icon);
            iv_apptb = (ImageView) itemView.findViewById(R.id.iv_apptb);
            item_close = (LinearLayout) itemView.findViewById(R.id.item_close);
            name = (TextView) itemView.findViewById(R.id.item_app_name);
        }
    }
}